//
//  SearchDetailsViewController.m
//  EcShop
//
//  Created by soft on 2017/11/6.
//  Copyright © 2017年 soft. All rights reserved.
//

#import "SearchDetailsViewController.h"
#import "ProductDetails.h"
#import "Searchask.h"
#import "HTTPMananger.h"
#import "classToDict.h"
#import "IndexTableViewCell.h"
#import "productDetailsViewController.h"
//#import "PYSearchConst.h"

@interface SearchDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSArray<ProductDetails *> *productList;
@property (nonatomic,strong) UITableView *taleV;
@end

@implementation SearchDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self searchProductList];
    //self.view.backgroundColor = PYSEARCH_RANDOM_COLOR;
    // Do any additional setup after loading the view.
}
-(void)tableV{
    self.taleV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.taleV.center =CGPointMake(self.view.bounds.size.width*0.5, self.view.bounds.size.height*0.6);
    //self.taleV.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.taleV];
    self.taleV.delegate = self;
    self.taleV.dataSource = self;
    
}
-(void)searchProductList{
    Searchask * sek = [Searchask new];
    sek.keyword = self.title;
    sek.orderby = @"nil";
    sek.page = @"nil";
    sek.offset = @"nil";
    NSDictionary *dict = [ classToDict getObjectData:sek];
    [[HTTPMananger sharedManager] searchIndex:^(NSDictionary *object, NSError *error) {
        if (error) {
            
            return;
        }
//        HIDE_HUD;
//        [Utility showMsg:@"接口访问成功！"];
        NSArray<ProductDetails *> *pl = [NSArray new];
        pl = [ProductDetails mj_objectArrayWithKeyValuesArray:object[@"productlist"]];
        self.productList = [ProductDetails mj_objectArrayWithKeyValuesArray:object[@"productlist"]];
        NSLog(@"%@",object);
//        dispatch_async(dispatch_get_main_queue(), ^{
//            //回调或者说是通知主线程刷新，
//            [self ScrollIma];
            [self tableV];
//
//        });
        
    } productPage:dict];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.productList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    // 从重用队列里查找可重用的cell
    IndexTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // 判断如果没有可以重用的cell，创建
    if (!cell) {
        cell = [[IndexTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.nameLbl.text =  self.productList[indexPath.row].name;
    
    cell.priceLbl.text =  cell.priceLbl.text = [NSString stringWithFormat:@"￥:%@",self.productList[indexPath.row].price];
    
    cell.descriptionsLbl.text = self.productList[indexPath.row].descriptions[@"desc"];
    NSArray *imas =self.productList[indexPath.row].pics;
    
    [self download:cell ImageWithUrl:imas[0][@"url"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    productDetailsViewController *prv = [[productDetailsViewController alloc]init];
    prv.prodtD = self.productList[indexPath.row];
    [self.navigationController pushViewController:prv animated:YES];
}
//异步线程加载网络下载图片 ——> 回到主线程更新UI
-(void)download:(IndexTableViewCell *)cell ImageWithUrl:(NSString *)imageDownloadURLStr{
    //以便在block中使用
    __block UIImage *image = [[UIImage alloc] init];
    //图片下载链接
    NSString *url = [NSString stringWithFormat:@"https://cdnme.azurewebsites.net%@",imageDownloadURLStr];
    NSURL *imageDownloadURL = [NSURL URLWithString:url];
    
    //将图片下载在异步线程进行
    //创建异步线程执行队列
    dispatch_queue_t asynchronousQueue = dispatch_queue_create("imageDownloadQueue", NULL);
    //创建异步线程
    dispatch_async(asynchronousQueue, ^{
        //网络下载图片  NSData格式
        NSError *error;
        NSData *imageData = [NSData dataWithContentsOfURL:imageDownloadURL options:NSDataReadingMappedIfSafe error:&error];
        if (imageData) {
            image = [UIImage imageWithData:imageData];
        }
        //回到主线程更新UI
        dispatch_async(dispatch_get_main_queue(), ^{
           // [_imageView setImage:image];
            cell.ima.image = image;
        });
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
